package com.hdu.controller.authorityManagement;

import com.hdu.entity.Account;
import com.hdu.entity.Role;
import com.hdu.pojo.PageBean;
import com.hdu.pojo.Result;
import com.hdu.service.IAccountService;
import com.hdu.service.IRoleService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户实体类 前端控制器
 * </p>
 *
 * @author xiaobai
 * @since 2023-10-28
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "用户管理接口")
@RequestMapping("/user")
public class UserController {

    private final IAccountService userService;

    private final IRoleService roleService;

    @GetMapping
    public Result<?> page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "9") Integer limit,
                       String username) {
        log.info("分页查询, 参数: {},{},{}", page, limit, username);
        //调用service分页查询
        PageBean<Account> pageBean = userService.page(page, limit, username);
        return Result.success(pageBean);
    }

    @DeleteMapping
    public Result<?> delete(@RequestBody List<String> idList) {
        log.info("删除用户, idList:{}", idList);
        userService.removeBatchByIds(idList);
        return Result.success();
    }

    @PostMapping
    public Result<?> append(@RequestBody Account account) {
        log.info("新增用户, user: {}", account);
        userService.append(account);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Account account) {
        log.info("更新角色信息 : {}", account);
        userService.update(account);
        return Result.success();
    }

    @GetMapping("/roleList/{id}")
    public Result<?> getMenuInfo(@PathVariable String id) {
        String roleAssign = userService.getById(id).getRole();
        List<Role> roleList = roleService.getRoleInfo(roleAssign, "");
        log.info("查询用户绑定角色信息：{}", roleList);
        return Result.success(roleList);
    }

    @PostMapping("/assign")
    public Result<?> assign(String userId, String[] roleId) {
        log.info("分配角色菜单信息 : {}", (Object) roleId);
        userService.assign(userId, roleId);
        return Result.success();
    }
}
